/*
 * @Author: Jiraiya
 * @Date: 2020-05-22 18:51:33
 * @LastEditors: Jiraiya
 * @LastEditTime: 2020-05-22 19:10:35
 * @Description: 
 */


ReactDOM = {
    render(element, container, callback) { //1
        return legacyRenderSubtreeIntoContainer(
            null,
            element,
            container,
            false,
            callback,
        );
    }
}

function legacyRenderSubtreeIntoContainer(parentComponent, children, container, forceHydrate, callback) {
    let root = container._reactRootContainer; //首次根元素没有该属性
    if (!root) { //2

        // new ReactRoot并创建了FiberRoot  3 4 5
        root = container._reactRootContainer = legacyCreateRootFromDOMContainer(
            container,
            forceHydrate, // 首次false
        );
        if (typeof callback === 'function') {
            const originalCallback = callback;
            callback = function () {
                const instance = DOMRenderer.getPublicRootInstance(root._internalRoot);
                originalCallback.call(instance);
            };
        }
        // 立即执行
        DOMRenderer.unbatchedUpdates(() => { // 6
            if (parentComponent != null) {
                root.legacy_renderSubtreeIntoContainer( // 7
                    parentComponent,
                    children,
                    callback,
                );
            } else {
                root.render(children, callback);
            }
        });
    }

}